import { AfterViewInit, ChangeDetectionStrategy, Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Common } from '@shared';
import { environment } from '../environments/environment';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { '[class.live-design]': 'true' },
})
export class AppComponent implements AfterViewInit {
  constructor(private http: HttpClient) {}

  ngAfterViewInit(): void {
    this.iconsResource('operate.svg');
    this.iconsResource('business.svg');
    this.iconsResource('illustration.svg');
    // @ts-ignore
    window.hwa?.('trackPageView');
  }

  iconsResource(type: string) {
    const base = location.href.slice(
      0,
      location.href.indexOf(Common.BASE_URL) >= 0 ? location.href.indexOf(Common.BASE_URL) : location.href.length
    );
    const prefix = `${environment.baseUrl}`;
    const url = `${base}${prefix}assets/icon/${type}`;
    const res = this.http.get(url, { responseType: 'text', params: { $prefix: 'none' } });

    res.subscribe(response => {
      document.body.insertAdjacentHTML('afterbegin', response);
    });
  }
}
